<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="//gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.68.3" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>基于Face&#43;&#43;实现简单的换脸效果 &middot; 我的博客</title>

  
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-0b ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/my_technology_blog/"><h1>我的博客</h1></a>
      <p class="lead">
       杨博的博客 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/my_technology_blog/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2021. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>基于Face&#43;&#43;实现简单的换脸效果</h1>
  <time datetime=2020-06-23T16:22:45&#43;0800 class="post-date">Tue, Jun 23, 2020</time>
  <p>​		<strong>Face++（旷视）通过提供云端API、离线SDK的自主研发产品形式，将人脸识别技术广泛应用到互联网及移动应用场景中，人脸识别云计算平台市场前景广阔。</strong></p>
<h2 id="前提">前提</h2>
<p>**1、我们先进入<a href="https://www.faceplusplus.com.cn/">Face++</a>网站，在控制台的应用管理选择 API Key ，并且创建一个 API Key **</p>
<p><strong>2、安装了这三个包：</strong></p>
<ul>
<li>requests</li>
<li>simplejson</li>
<li>json</li>
<li>base64</li>
</ul>
<p><strong>3、找好两张图片，和修改好图片的存放位置</strong></p>
<p><strong>导包</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">import</span> requests
<span style="color:#f92672">import</span> simplejson
<span style="color:#f92672">import</span> json
<span style="color:#f92672">import</span> base64
</code></pre></div><p><strong>创建获取人脸关键点函数</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#66d9ef">def</span> <span style="color:#a6e22e">find_face</span>(imgpath):
    <span style="color:#66d9ef">print</span>(<span style="color:#e6db74">&#34;正在查找……&#34;</span>)
    http_url <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;https://api-cn.faceplusplus.com/facepp/v3/detect&#39;</span>
    data <span style="color:#f92672">=</span> {<span style="color:#e6db74">&#34;api_key&#34;</span>: <span style="color:#e6db74">&#39;申请的API Key&#39;</span>,
            <span style="color:#e6db74">&#34;api_secret&#34;</span>: <span style="color:#e6db74">&#39;申请的API Secret&#39;</span>,
            <span style="color:#e6db74">&#34;image_url&#34;</span>: imgpath, <span style="color:#e6db74">&#34;return_landmark&#34;</span>: <span style="color:#ae81ff">1</span>}
    files <span style="color:#f92672">=</span> {<span style="color:#e6db74">&#34;image_file&#34;</span>: open(imgpath, <span style="color:#e6db74">&#34;rb&#34;</span>)}
    response <span style="color:#f92672">=</span> requests<span style="color:#f92672">.</span>post(http_url, data<span style="color:#f92672">=</span>data, files<span style="color:#f92672">=</span>files)
    req_con <span style="color:#f92672">=</span> response<span style="color:#f92672">.</span>content<span style="color:#f92672">.</span>decode(<span style="color:#e6db74">&#39;utf-8&#39;</span>)
    req_dict <span style="color:#f92672">=</span> json<span style="color:#f92672">.</span>JSONDecoder()<span style="color:#f92672">.</span>decode(req_con)
    this_json <span style="color:#f92672">=</span> simplejson<span style="color:#f92672">.</span>dumps(req_dict)

    this_json2 <span style="color:#f92672">=</span> simplejson<span style="color:#f92672">.</span>loads(this_json)
    <span style="color:#66d9ef">print</span>(this_json2)
    faces <span style="color:#f92672">=</span> this_json2[<span style="color:#e6db74">&#39;faces&#39;</span>]
    list0 <span style="color:#f92672">=</span> faces[<span style="color:#ae81ff">0</span>]
    rectangle <span style="color:#f92672">=</span> list0[<span style="color:#e6db74">&#39;face_rectangle&#39;</span>]
    <span style="color:#75715e">#print(rectangle)</span>
    <span style="color:#66d9ef">return</span> rectangle
</code></pre></div><p><strong>开始换脸（照片大小最好小于2M，number为换脸的相似度）</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#66d9ef">def</span> <span style="color:#a6e22e">merge_face</span>(image_url1, image_url2, image_url, number):

    ff1 <span style="color:#f92672">=</span> find_face(image_url1)
    ff2 <span style="color:#f92672">=</span> find_face(image_url2)

    rectangle1 <span style="color:#f92672">=</span> str(str(ff1[<span style="color:#e6db74">&#39;top&#39;</span>]) <span style="color:#f92672">+</span> <span style="color:#e6db74">&#34;,&#34;</span> <span style="color:#f92672">+</span> str(ff1[<span style="color:#e6db74">&#39;left&#39;</span>]) <span style="color:#f92672">+</span>
                     <span style="color:#e6db74">&#34;,&#34;</span> <span style="color:#f92672">+</span> str(ff1[<span style="color:#e6db74">&#39;width&#39;</span>]) <span style="color:#f92672">+</span> <span style="color:#e6db74">&#34;,&#34;</span> <span style="color:#f92672">+</span> str(ff1[<span style="color:#e6db74">&#39;height&#39;</span>]))
    rectangle2 <span style="color:#f92672">=</span> str(ff2[<span style="color:#e6db74">&#39;top&#39;</span>]) <span style="color:#f92672">+</span> <span style="color:#e6db74">&#34;,&#34;</span> <span style="color:#f92672">+</span> str(ff2[<span style="color:#e6db74">&#39;left&#39;</span>]) <span style="color:#f92672">+</span> \
        <span style="color:#e6db74">&#34;,&#34;</span> <span style="color:#f92672">+</span> str(ff2[<span style="color:#e6db74">&#39;width&#39;</span>]) <span style="color:#f92672">+</span> <span style="color:#e6db74">&#34;,&#34;</span> <span style="color:#f92672">+</span> str(ff2[<span style="color:#e6db74">&#39;height&#39;</span>])
    <span style="color:#66d9ef">print</span>(rectangle2)
    url_add <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;https://api-cn.faceplusplus.com/imagepp/v1/mergeface&#34;</span>
    f1 <span style="color:#f92672">=</span> open(image_url1, <span style="color:#e6db74">&#39;rb&#39;</span>)
    f1_64 <span style="color:#f92672">=</span> base64<span style="color:#f92672">.</span>b64encode(f1<span style="color:#f92672">.</span>read())
    f1<span style="color:#f92672">.</span>close()
    f2 <span style="color:#f92672">=</span> open(image_url2, <span style="color:#e6db74">&#39;rb&#39;</span>)
    f2_64 <span style="color:#f92672">=</span> base64<span style="color:#f92672">.</span>b64encode(f2<span style="color:#f92672">.</span>read())
    f2<span style="color:#f92672">.</span>close()

    data <span style="color:#f92672">=</span> {<span style="color:#e6db74">&#34;api_key&#34;</span>: <span style="color:#e6db74">&#39;8UijmDAywpGZEaonC-PRDCLXUIlElU1L&#39;</span>,
            <span style="color:#e6db74">&#34;api_secret&#34;</span>: <span style="color:#e6db74">&#39;skDeA_pgvKZloCdHK55pWaXQK4Rol7JC&#39;</span>,
            <span style="color:#e6db74">&#34;template_base64&#34;</span>: f1_64, <span style="color:#e6db74">&#34;template_rectangle&#34;</span>: rectangle1,
            <span style="color:#e6db74">&#34;merge_base64&#34;</span>: f2_64, <span style="color:#e6db74">&#34;merge_rectangle&#34;</span>: rectangle2, <span style="color:#e6db74">&#34;merge_rate&#34;</span>: number}

    response <span style="color:#f92672">=</span> requests<span style="color:#f92672">.</span>post(url_add, data<span style="color:#f92672">=</span>data)
    req_con1 <span style="color:#f92672">=</span> response<span style="color:#f92672">.</span>content<span style="color:#f92672">.</span>decode(<span style="color:#e6db74">&#39;utf-8&#39;</span>)
    <span style="color:#75715e">#print(req_con1)</span>
    req_dict <span style="color:#f92672">=</span> json<span style="color:#f92672">.</span>JSONDecoder()<span style="color:#f92672">.</span>decode(req_con1)
    result <span style="color:#f92672">=</span> req_dict[<span style="color:#e6db74">&#39;result&#39;</span>]
    imgdata <span style="color:#f92672">=</span> base64<span style="color:#f92672">.</span>b64decode(result)
    file <span style="color:#f92672">=</span> open(image_url, <span style="color:#e6db74">&#39;wb&#39;</span>)
    file<span style="color:#f92672">.</span>write(imgdata)
    file<span style="color:#f92672">.</span>close()


image1 <span style="color:#f92672">=</span> <span style="color:#e6db74">r</span><span style="color:#e6db74">&#34;要换的脸路径&#34;</span>
image2 <span style="color:#f92672">=</span> <span style="color:#e6db74">r</span><span style="color:#e6db74">&#34;要换脸的人路径&#34;</span>
image <span style="color:#f92672">=</span> <span style="color:#e6db74">r</span><span style="color:#e6db74">&#34;C:\Users\yangbo\Pictures\换脸\换好1.0.jpg&#34;</span>		<span style="color:#75715e"># 换好之后的图片存储路径</span>

merge_face(image2, image1, image, <span style="color:#ae81ff">90</span>)
</code></pre></div><p>​</p>

</div>


    </main>

    
  </body>
</html>
